const mongoose = require("mongoose");

mongoose
  .connect("mongodb://127.0.0.1:27017/online-pharmacy")
  .then(() => {
    console.log("数据库连接成功");
  })
  .catch((err) => {
    console.log("数据库连接失败", err);
  });
// 用户表
const userSchema = new mongoose.Schema({
  userId: String,
  username: String,
  password: String,
  phone: String,
});

// 药品分类表
const categorySchema = new mongoose.Schema({
  label: String,
  value: String
});
// 药品表
const medicineSchema = new mongoose.Schema({
  // medicineId: String,
  id: String,
  name: String,
  price: Number,
  description: String,
  image: String,
  category: String,
  brand: String,
  dosga: String,
  ingredients: String,
  sideEffect: String,
  storage: String,
  expiryDate: String,
});
// 订单表
// const orderSchema = new mongoose.Schema({
//   orderId: String,
//   user: {
//     type: mongoose.Schema.Types.ObjectId,
//     ref: "User",
//   },
//   medicines: [
//     {
//       type: mongoose.Schema.Types.ObjectId,
//       ref: "Medicine",
//     },
//   ],
//   totalPrice: Number,
//   status: String,
// });
// 配送表
const deliverySchema = new mongoose.Schema({
  deliverId: String,
  orderId: String,
  address: String,
  status: String,
});

const gou = new mongoose.Schema({
  title: String,
  context: String,
  price: Number,
  img: String,
});

const jia = new mongoose.Schema({
  title: String,
  context: String,
  price: Number,
  img: String,
});

const di = new mongoose.Schema({
  name: String,
  phone: String,
  address: String,
  xiang: String,
  mo: {
    type: Boolean,
    default: false,
  },
  top: {
    type: Boolean,
    default: false,
  },
});
const orderSchema = new mongoose.Schema({
  id: String,
  status: String,
  statusText: String,
  statusTip: String,
  medicine: {
    name: String,
    spec: String,
    img: String,
    count: Number,
    price: Number,
  },
  total: Number,
  coupon: Number,
  freight: Number,
  pay: Number,
  orderNo: String,
  orderTime: String,
  paymentMethod: String, // 支付方式: wechat/alipay
  paymentStatus: String, // 支付状态: unpaid/pending/paid
  receiver: String,
  phone: String,
  address: String,
  remark: String,
  logistics: {
    company: String,
    number: String,
    traces: [
      {
        time: String,
        status: String,
        location: String,
      },
    ],
  },
});
const orderModel = mongoose.model("order", orderSchema, "order");

const gouModel = mongoose.model("gou", gou, "gou");

const jiaModel = mongoose.model("jia", jia, "jia");

const diModel = mongoose.model("di", di, "di");

const User = mongoose.model("User", userSchema, "User");

const Category = mongoose.model("Category", categorySchema, "Category");

const Medicine = mongoose.model("Medicine", medicineSchema, "Medicine");


const Delivery = mongoose.model("Delivery", deliverySchema, "Delivery");

module.exports = {
  User,
  Category,
  Medicine,
  orderModel,
  Delivery,
  gouModel,
  jiaModel,
  diModel,
  mongoose,
  orderSchema,
};
